

#import "BBTabBarController.h"

#import "BBNavigationController.h"
    
#import "AJSoundManager.h"
#import "HomeViewController.h"

#import "ChoicenessViewController.h"

#import "MessageViewController.h"


#import "MyViewController.h"

@interface BBTabBarController ()

@end
#define zTabHeight               (IS_IPHONE_X ? 83 : 49)
@implementation BBTabBarController{
    NSNumber * addFriendCount;
    NSInteger * bageValue;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noti:) name:POSTTABBARCOUNT object:nil];
    //注册好友回调
     
    bageValue=0;
    self.tabBar.barTintColor = ColorMain;
    [self setUpAllChildViewController];
}
-(void)viewDidLayoutSubviews {
    //
    [super viewDidLayoutSubviews];
    
    
    

}

- (void)setUpAllChildViewController {
    
    HomeViewController *home = [[HomeViewController alloc] init];
    [self setUpOneChildViewController:home image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"homeSelet"] title:@"首页"];
    ChoicenessViewController *choiceness = [[ChoicenessViewController alloc] init];
    [self setUpOneChildViewController:choiceness image:[UIImage imageNamed:@"choice"] selectedImage:[UIImage imageNamed:@"choiceSelet"] title:@"精选"];
    MessageViewController *message = [[MessageViewController alloc] init];
    [self setUpOneChildViewController:message image:[UIImage imageNamed:@"message"] selectedImage:[UIImage imageNamed:@"messageSelet"] title:@"消息"];
    MyViewController *myV = [[MyViewController alloc] init];
    [self setUpOneChildViewController:myV image:[UIImage imageNamed:@"Mine"] selectedImage:[UIImage imageNamed:@"MineSelet"] title:@"我的"];
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    view.frame = CGRectMake(0, 0, WIDTH, kTabHeight+44);
    [[UITabBar appearance] insertSubview:view atIndex:0];
    
    //去黑线
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [UITabBar appearance].layer.borderWidth = 0.0f;
    [UITabBar appearance].clipsToBounds = YES;
    [UITabBar appearance].tintColor = ColorMain;
}

- (void)setUpOneChildViewController:(UIViewController *)vc image:(UIImage *)image selectedImage:(UIImage *)selectedImage title:(NSString *)title {
    
    vc.title = title;
    if ([title isEqualToString:@"首页"]) {
        vc.navigationItem.title = @"";
    }else if ([title isEqualToString:@"我的2"]){
        vc.navigationController.navigationBar.hidden = YES;
        
        vc.navigationItem.title = @"";
    }
    vc.tabBarItem.image = image;
    vc.tabBarItem.selectedImage = selectedImage;
    if ( @available(iOS 13.0, *)) {
        
    }else
    {
        vc.tabBarItem.imageInsets=UIEdgeInsetsMake(5,0,-5,0);

    }
    BBNavigationController *nav = [[BBNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}
-(void)noti:(NSNotification *)noti{
    addFriendCount=noti.userInfo[@"count"];
    
}
-(void)friendRequestDidReceiveFromUser:(NSString *)aUsername message:(NSString *)aMessage{
    //定义两个变量控制bageValue
    if (bageValue ==  nil || bageValue == NULL || addFriendCount == nil || addFriendCount == NULL) {
        bageValue = 0;
        addFriendCount = [NSNumber numberWithInteger:0];
        return ;
    }
    bageValue++;
    NSInteger page=*([addFriendCount integerValue]+bageValue);
    addFriendCount=[NSNumber numberWithInteger:page];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
        if ([addFriendCount intValue]>0) {
            
            item.badgeValue=[NSString stringWithFormat:@"%@",addFriendCount];
        }else{
            item.badgeValue=nil;
        }
    });
    
    //需把变量制空,防止重复
    bageValue=0;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
